package com.dj.shop.service.impl;

import com.dj.shop.mapper.GoodsFlowMapper;
import com.dj.shop.mapper.OrderFlowMapper;

import com.dj.shop.model.GoodsFlow;
import com.dj.shop.model.OrderFlow;

import com.dj.shop.model.OrderWrapper;
import com.dj.shop.service.OrderService;
import com.dj.shop.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author renshp
 * @Title: OrderServiceImpl
 * @Description: TODO
 * @date 2019/3/5
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderFlowMapper orderFlowMapper;

    @Autowired
    GoodsFlowMapper goodsFlowMapper;

    @Override
    public int addOrder(OrderFlow orderFlow, List<GoodsFlow> goodsFlowList) {


        // 获取系统日期、系统时间
        String nowDate = DateUtil.getNowDate();
        orderFlow.setCreateDatetime(nowDate);

        // 新增订单流水
        orderFlow.setOrderStatus("0");  // 默认为"0-待付款"

        int result = orderFlowMapper.insertSelective(orderFlow);

        if (result == 0){
            return 0;
        }

        for (int i=0; i<goodsFlowList.size(); i++){

            // 循环信息商品流水
            GoodsFlow goodsFlow = goodsFlowList.get(i);
            goodsFlow.setUserId(orderFlow.getUserId());
            goodsFlow.setShopId(orderFlow.getShopId());
            goodsFlow.setOrderNo(orderFlow.getOrderNo());
            goodsFlow.setTransType(orderFlow.getOrderType());
            goodsFlow.setGoodsWeight(12);
            goodsFlow.getGoodsId();
            goodsFlow.setTransDatetime(nowDate);
            goodsFlow.setTransStatus("0");  // 默认为"0-待付款"

            result = goodsFlowMapper.insertSelective(goodsFlow);

            if (result == 0){
                return 0;
            }
        }

        return 1;
    }

    @Override
    public int updateOrderStatus(int orderId, String orderStatus){

        OrderFlow orderFlow = new OrderFlow();
        orderFlow.setOrderNo(orderId);
        orderFlow.setOrderStatus(orderStatus);

        return orderFlowMapper.updateByPrimaryKeySelective(orderFlow);
    }

    @Override
    public OrderFlow queryOrderById(int orderNo) {
        return orderFlowMapper.selectByPrimaryKey(orderNo);
    }

    @Override
    public List<GoodsFlow> queryGoodsListByOrderNo(int orderNo) {
        return goodsFlowMapper.selectByOrderNo(orderNo);
    }

    @Override
    public List<OrderWrapper> queryOrder(Map<String,Object> orderes){return orderFlowMapper.queryOrder(orderes);
    }
}
